home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: Passing functions as parameters... inconsistent behaviour
- Date: 24 Feb 1996 04:23:22 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb23212322@qcd.lanl.gov>
- References: <4glbau$tge@inet.up.ac.za>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Rudolph Pienaar's message of 23 Feb 1996 21:24:46 GMT
-
- <snip>
- RP: void test(float (*passedfunc)()) {
- <snip>
- RP: float passed(float a){
- <snip>
- RP: test( passed );
- <snip>
- RP: ptr.c: In function `main':
- RP: ptr.c:17: warning: passing arg 1 of `test' from incompatible pointer type
- RP:
- RP: Of course, this is just a sample... the general rule seems to be that if a
- RP: function is passed to another, and if this passed function has any
- parameters
- RP: of its own other than *int* (and this includes user defined
- types), compilation
- RP: results in a similar warning.
-
- You are misjudging the problem: your mistake is that you are mixing
- `old' and `new' style declarations. If you had declared test as
- void test(float (*passedfunc)(float))
- you would have not seen any problem!
-
- Essentially, () in a declaration is an old style declaration. It says
- the function in question takes a fixed number of parameters and none
- the parameters (if any) are `promotable', i.e. they are not varieties
- of char, varieties of short, enums or floats. Do you see the problem
- now?
-
- Now why is this? Well, before the `new' style declaration came along,
- functions were declared with (), except during definition, when one
- used a list of variables without types. A typical definition looked
- like
-
- float passed(x)
- float x;
- {/* body */}
-
- Now, though this looks remarkably like
-
- float passed(float x)
- {/* body */}
-
- there is a very important distinction. Apart from the fact that the
- old form of declaration relieves the compiler of any responsibility to
- actually check that the number and types of objects passed matched the
- declaration, there is another very important difference. The old form
- says that when `passed' is called, it is actually passed a double
- which is converted _by it_ to a float. (Similarly, char and short are
- passed as int instead) The new style declaration on the other hand
- says that the compiler can pass the float as a float. Thus, contrary
- to appearances, the `old' form is not compatible with `float
- passed(float)', but rather with `float passed(double)'. In fact, it is
- not posible to write an old style declaration compatible with `float
- passed(float)'.
-
- Because of this historical state of affairs, `float passed()'
- _is_ compatible with `float passed(double)', but not with `float
- passed(float)'.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-